package xyz.xtt.exchange.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import jakarta.validation.constraints.Max;
import xyz.xtt.common.response.ApiResult;
import xyz.xtt.exchange.service.IUserCollectService;

/**
 * <p>
 * 用户收藏表 前端控制器
 * </p>
 *
 * @author author
 * @since 2024-12-28
 */
@RestController
@RequestMapping("/api/exchange/user_collect")
public class UserCollectController {

	@Autowired
	private IUserCollectService userCollectService;

	@GetMapping("/get_status")
	public ApiResult<?> getStatus(@RequestHeader("uid") String uid, String signId, int signType) {
		Long userId = Long.parseLong(uid);
		if (0L == userId) {
			return ApiResult.ok(false);
		}
		return ApiResult.ok(userCollectService.getStatus(userId, signId, signType));
	}

	@GetMapping("/edit")
	// status 0 收藏， 1 取消收藏
	public ApiResult<?> edit(@RequestHeader("uid") String uid, @RequestParam("signId") String signId, @RequestParam("signType") int signType,
			@RequestParam("status") int status) {
		Long userId = Long.parseLong(uid);
		boolean isCollect = userCollectService.getStatus(userId, signId, signType);
		if (status == 0) {
			if (isCollect) {
				return ApiResult.ok(true);
			}
			return ApiResult.ok(userCollectService.add(userId, signId, signType));
		}
		if (status == 1) {
			if (!isCollect) {
				return ApiResult.ok(true);
			}
			return ApiResult.ok(userCollectService.del(userId, signId, signType));
		}
		return ApiResult.ok(false);
	}

	@GetMapping("/getAllCollectUser")
	public ApiResult<?> getAllCollect(@RequestHeader("uid") String uid, @RequestParam(value = "currPage", defaultValue = "0") Integer currPage,
			@RequestParam(value = "pageSize", defaultValue = "10") @Max(value = 100, message = "每页数量不能超过100") Integer pageSize) {
		Long userId = Long.parseLong(uid);
		if (0L == userId) {
			return ApiResult.ok(false);

		}
		return ApiResult.ok(userCollectService.getAllCollect(userId, currPage, pageSize));
	}

}
